package com.mysns.web.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mysns.core.dao.GenericUserDao;
import com.mysns.core.domain.GenericUser;

/**
 * Ajax 的控制器.
 * 
 * @author zhouych
 */
@Controller
@RequestMapping("/ajax")
public class AjaxController {

	/**
	 * Logger
	 */
	private static Logger logger = LoggerFactory.getLogger(AccountController.class);
	
	/**
	 * 操作用户的 Dao
	 */
	@Resource
	private GenericUserDao genericUserDao;
	
	
	/**
	 * ajax 方式检测用户注册名是否重复.
	 * 
	 * @param request
	 * @param response
	 * @param loginName
	 * @throws Exception
	 */
	@RequestMapping(value="/validate/loginname")
	public void validateLoginName(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "loginName", required = true) String loginName)
			throws Exception {
		
		if (loginName != null) {
			GenericUser user = genericUserDao.findUniqueBy("loginName", loginName);
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/plain;charset=utf-8");
			if (user != null) {
				response.getWriter().write("err");
			} else {
				response.getWriter().write("");
			}
		}
	}
	
	/**
	 * ajax 方式检测邮箱是否重复.
	 * 
	 * @param request
	 * @param response
	 * @param email
	 * @throws Exception
	 */
	@RequestMapping(value="/validate/email")
	public void validateEmail(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "email", required = true) String email)
			throws Exception {
		
		if (email != null) {
			GenericUser user = genericUserDao.findUniqueBy("email", email);
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/plain;charset=utf-8");
			if (user != null) {
				response.getWriter().write("err");
			} else {
				response.getWriter().write("");
			}
		}
	}
	
	/**
	 * ajax 方式检测验证码是否正确.
	 * 
	 * @param request
	 * @param response
	 * @param checkcode
	 * @throws Exception
	 */
	@RequestMapping(value="/validate/checkcode")
	public void validateCheckcode(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "checkcode", required = true) String checkcode)
			throws Exception {
		
		String correctCode = (String) request.getSession().getAttribute(
				com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain;charset=utf-8");
		if (StringUtils.isBlank(checkcode) || !checkcode.trim().equals(correctCode)) {
			response.getWriter().write("err");
		} else {
			response.getWriter().write("");
		}
	}
}
